﻿<UserControl    x:Class="IBTrader.Frontend.Trader.Control"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
                xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
                xmlns:ctrlConn="clr-namespace:IBTrader.Frontend.Connection"
                xmlns:ctrlLog="clr-namespace:IBTrader.Frontend.Log"
                xmlns:my="clr-namespace:IBTrader.Frontend.Trader"
                xmlns:con="http://schemas.kent.boogaart.com/converters"
                xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" 
                xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
                xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock">
    <UserControl.DataContext>
        <my:ViewModel/>
    </UserControl.DataContext>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding LoadedCommand}" 
                                   CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="Closing" SourceObject="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}">
            <i:InvokeCommandAction Command="{Binding UnloadedCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <UserControl.Resources>
        <my:SourceConverter x:Key="SourceConverter"/>
        <xcdg:InverseBooleanConverter x:Key="InverseBooleanConverter"/>
    </UserControl.Resources>
    <DockPanel LastChildFill="True">
        <Menu Height="22" DockPanel.Dock="Top">
            <ctrlConn:Menu />
            <MenuItem Header="Algo">
                <MenuItem Header="Start" Command="{Binding Start}"/>
                <MenuItem Header="Cancel" Command="{Binding Cancel}"/>
                <MenuItem Header="Reset" Command="{Binding Reset}"/>
            </MenuItem>
            <ctrlLog:Menu />
        </Menu>
        <ctrlLog:Errors DockPanel.Dock="Top" />
        <ctrlLog:Status DockPanel.Dock="Bottom" />
        <xcad:DockingManager>
            <xcad:LayoutRoot>
                <xcad:LayoutPanel Orientation="Horizontal">
                    <xcad:LayoutPanel Orientation="Vertical" DockWidth="30*">
                        <xcad:LayoutAnchorablePane DockMinHeight="125">
                            <xcad:LayoutAnchorable Title="Selected Source" CanHide="False" CanClose="False">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="22"/>
                                        <RowDefinition Height="22"/>
                                        <RowDefinition Height="22"/>
                                        <RowDefinition Height="37"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="80"/>
                                        <ColumnDefinition Width="60"/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <RadioButton Grid.Row="0" GroupName="Source" Content="Live" Name="LiveSource" Margin="10,0,0,0" IsChecked="{Binding Path=Source,  
                                        Converter={StaticResource SourceConverter}, ConverterParameter=IBTrader.Modules.Read.ReaderRealTime}" VerticalAlignment="Center"/>
                                    <RadioButton Grid.Row="1" GroupName="Source" Content="BackLog" Margin="10,0,0,0" IsChecked="{Binding Path=Source, 
                                        Converter={StaticResource SourceConverter}, ConverterParameter=Backlog}" VerticalAlignment="Center"
                                        IsEnabled="{Binding IsConnected,Converter={StaticResource InverseBooleanConverter}}" />
                                    <CheckBox Grid.Column="1" Content="Armed" VerticalAlignment="Center" IsEnabled="{Binding ElementName=LiveSource, Path=IsChecked}" 
                                              IsChecked="{Binding IsConnected, Mode=OneWay}">
                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="Checked">
                                                <cmd:EventToCommand Command="{Binding Connect}"/>
                                            </i:EventTrigger>
                                            <i:EventTrigger EventName="Unchecked">
                                                <cmd:EventToCommand Command="{Binding Disconnect}"/>
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>
                                    </CheckBox>
                                    <TextBlock Grid.Column="2" Text="Strategy:" HorizontalAlignment="Right"  />
                                    <ComboBox Grid.Column="3" Margin="10,0,10,0" ItemsSource="{Binding Algo.Strategies}" SelectedValue="{Binding Algo.Strategy}" />
                                    <TextBlock Grid.Column="2" Grid.Row="1" Text="Indicator:" HorizontalAlignment="Right" />
                                    <ComboBox Grid.Column="3" Grid.Row="1" Margin="10,0,10,0" ItemsSource="{Binding Algo.Indicators}" 
                                              DisplayMemberPath="Name" SelectedValuePath="FullName" SelectedValue="{Binding Algo.Indicator}" />
                                    <TextBlock Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2" Text="Indicator Interval (seconds):" HorizontalAlignment="Right" />
                                    <xctk:DoubleUpDown Grid.Column="3" Grid.Row="2" Margin="10,0,10,0" Value="{Binding Algo.IndicatorInterval}"/>
                                    <UniformGrid Grid.Row="3" Grid.ColumnSpan="4" Columns="3" Height="30">
                                        <Button Content="Start" Command="{Binding Start}" Margin="10,5,10,5" />
                                        <Button Content="Cancel" Command="{Binding Cancel}" Margin="0,5,0,5" />
                                        <Button Content="Reset" Command="{Binding Reset}" Margin="10,5,10,5" />
                                    </UniformGrid>
                                </Grid>
                            </xcad:LayoutAnchorable>
                        </xcad:LayoutAnchorablePane>
                        <xcad:LayoutAnchorablePane DockHeight="60*">
                            <xcad:LayoutAnchorable Title="Filled Orders" CanHide="False" CanClose="False">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition />
                                        <RowDefinition Height="23"/>
                                    </Grid.RowDefinitions>
                                    <DataGrid Grid.Row="0" ItemsSource="{Binding Algo.Orders.Filled}" IsReadOnly="True" AutoGenerateColumns="False">
                                        <DataGrid.Columns>
                                            <DataGridTextColumn Header="Created" Binding="{Binding Created, StringFormat=\{0:HH:mm:ss\}}"/>
                                            <DataGridTextColumn Header="Action" Binding="{Binding Action}"/>
                                            <DataGridTextColumn Header="Type" Binding="{Binding OrderType}"/>
                                            <DataGridTextColumn Header="Entry" Binding="{Binding Type}"/>
                                            <DataGridTextColumn Header="Status" Binding="{Binding Status}"/>
                                            <DataGridTextColumn Header="Price" Binding="{Binding Price}"/>
                                            <DataGridTextColumn Header="#" Binding="{Binding Quantity}"/>
                                            <DataGridTextColumn Header="FillPrice" Binding="{Binding FillPrice}"/>
                                            <DataGridTextColumn Header="Filled" Binding="{Binding Filled}"/>
                                        </DataGrid.Columns>
                                    </DataGrid>
                                    <TextBlock Grid.Row="1" Text="{Binding Algo.Orders.Summary}"/>
                                </Grid>
                            </xcad:LayoutAnchorable>
                        </xcad:LayoutAnchorablePane>
                        <xcad:LayoutAnchorablePane DockHeight="30*">
                            <xcad:LayoutAnchorable Title="Backlog Configuration" CanHide="False" CanClose="False">
                                <GroupBox Header="Parameter">
                                    <Grid VerticalAlignment="Top">
                                        <Grid.RowDefinitions>
                                            <RowDefinition />
                                            <RowDefinition />
                                            <RowDefinition />
                                            <RowDefinition />
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition />
                                            <ColumnDefinition />
                                            <ColumnDefinition />
                                            <ColumnDefinition />
                                        </Grid.ColumnDefinitions>
                                        <Label Content="Source:" Grid.Row="0"/>
                                        <Label Content="Symbol:" Grid.Row="1"/>
                                        <Label Content="Date:" Grid.Row="2"/>
                                        <Label Content="Folder:" Grid.Row="3"/>
                                        <ComboBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="3" Name="FileFormat"
                                                  ItemsSource="{Binding Algo.BacklogConfiguration.FileFormats}" DisplayMemberPath="Text" SelectedValuePath="Value"
                                                  SelectedValue="{Binding Path=Algo.BacklogConfiguration.FileFormat}"/>
                                        <TextBox Grid.Row="1" Grid.Column="1" Name="Symbol" Text="{Binding Path=Algo.Contract.Symbol, UpdateSourceTrigger=PropertyChanged}"/>
                                        <Button Grid.Row="1" Grid.Column="2" Content="Previous Symbol" Command="{ Binding PreviousSymbol}"/>
                                        <Button Grid.Row="1" Grid.Column="3" Content="Next Symbol" Command="{ Binding NextSymbol}"/>
                                        <DatePicker Grid.Row="2" Grid.Column="1" Name="Date" SelectedDate="{Binding Path=Algo.BacklogConfiguration.Date}"/>
                                        <Button Grid.Row="2" Grid.Column="2" Content="Previous Date" Command="{ Binding PreviousDate}"/>
                                        <Button Grid.Row="2" Grid.Column="3" Content="Next Date" Command="{ Binding NextDate}"/>
                                        <TextBox Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="3" Name="Folder" Text="{Binding Path=Algo.BacklogConfiguration.Folder}"/>
                                    </Grid>
                                </GroupBox>
                            </xcad:LayoutAnchorable>
                            <xcad:LayoutAnchorable Title="Algo" CanHide="False" CanClose="False">
                                <xctk:PropertyGrid SelectedObject="{Binding Algo}" AutoGenerateProperties="False" ShowSummary="False" IsCategorized="False" 
                                                   ShowSearchBox="False" ShowSortOptions="False" ShowTitle="False">
                                    <xctk:PropertyGrid.PropertyDefinitions>
                                        <xctk:PropertyDefinition Name="OrderDuration" />
                                        <xctk:PropertyDefinition Name="OrderSize"/>
                                        <xctk:PropertyDefinition Name="OrderType"/>
                                        <xctk:PropertyDefinition Name="PriceOffset"/>
                                        <xctk:PropertyDefinition Name="StopLimit"/>
                                        <xctk:PropertyDefinition Name="TradingStart"/>
                                        <xctk:PropertyDefinition Name="TradingEnd"/>
                                        <xctk:PropertyDefinition Name="OptimisticSimulation"/>
                                    </xctk:PropertyGrid.PropertyDefinitions>
                                </xctk:PropertyGrid>
                            </xcad:LayoutAnchorable>
                            <xcad:LayoutAnchorable Title="Contract" CanHide="False" CanClose="False">
                                <xctk:PropertyGrid SelectedObject="{Binding Algo.Contract}" AutoGenerateProperties="False" ShowSummary="False" IsCategorized="False"
                                                   ShowSearchBox="False" ShowSortOptions="False" ShowTitle="False">
                                    <xctk:PropertyGrid.PropertyDefinitions>
                                        <xctk:PropertyDefinition Name="Currency"/>
                                        <xctk:PropertyDefinition Name="Exchange"/>
                                        <xctk:PropertyDefinition Name="Expiry"/>
                                        <xctk:PropertyDefinition Name="Increment"/>
                                        <xctk:PropertyDefinition Name="Multiplier"/>
                                        <xctk:PropertyDefinition Name="PrimaryExchange"/>
                                        <xctk:PropertyDefinition Name="SecurityType"/>
                                        <xctk:PropertyDefinition Name="Symbol"/>
                                    </xctk:PropertyGrid.PropertyDefinitions>
                                </xctk:PropertyGrid>
                            </xcad:LayoutAnchorable>
                            <xcad:LayoutAnchorable Title="Connection" CanHide="False" CanClose="False">
                                <ctrlConn:Control />
                            </xcad:LayoutAnchorable>
                        </xcad:LayoutAnchorablePane>
                    </xcad:LayoutPanel>
                    <xcad:LayoutPanel Orientation="Vertical" DockWidth="70*" >
                        <xcad:LayoutAnchorablePane DockHeight="70*">
                            <xcad:LayoutAnchorable Title="Chart" CanHide="False" CanClose="False">
                                <Image x:Name="Image" Stretch="Fill">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="SizeChanged">
                                            <cmd:EventToCommand Command="{Binding SizeChanged}"/>
                                        </i:EventTrigger>
                                        <i:EventTrigger EventName="IsVisibleChanged">
                                            <cmd:EventToCommand Command="{Binding IsVisibleChanged}"/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </Image>
                            </xcad:LayoutAnchorable>
                            <!--<xcad:LayoutAnchorable Title="Canvas" CanHide="False" CanClose="False">
                                <Grid Name="CanvasGrid" >
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="SizeChanged">
                                            <cmd:EventToCommand Command="{Binding SizeChanged}"/>
                                        </i:EventTrigger>
                                        <i:EventTrigger EventName="IsVisibleChanged">
                                            <cmd:EventToCommand Command="{Binding IsVisibleChanged}"/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                    <Canvas x:Name="Canvas" SnapsToDevicePixels="True"/>
                                </Grid>
                            </xcad:LayoutAnchorable>-->
                        </xcad:LayoutAnchorablePane>
                        <xcad:LayoutAnchorablePane DockHeight="30*">
                            <xcad:LayoutAnchorable Title="Log" CanHide="False" CanClose="False">
                                <ctrlLog:Control />
                            </xcad:LayoutAnchorable>
                        </xcad:LayoutAnchorablePane>
                    </xcad:LayoutPanel>
                </xcad:LayoutPanel>
            </xcad:LayoutRoot>
        </xcad:DockingManager>
    </DockPanel>
</UserControl>
